package com.rose.doorto.footbathdoor.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rose.doorto.footbathdoor.enums.MessageStatusEnum;
import com.rose.doorto.footbathdoor.mapper.QueueMessagesMapper;
import com.rose.doorto.footbathdoor.pojo.QueueMessages;
import com.rose.doorto.footbathdoor.service.QueueMessagesService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

/**
 * 定时任务,每隔10秒去数据库查询,然后在从新投递到mq
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.footbathdoor.task
 * @Project：door-to-door
 * @name：RabbitSendTask
 * @date 2024/09/05 2024/9/5:14:38:
 */
@Component
//@EnableScheduling
public class RabbitSendTask {

    @Autowired
    private QueueMessagesMapper queueMessagesMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private QueueMessagesService queueMessagesService;

    //每隔10秒执行一次,根据状态为0并且重试时间小于当前时间的消息记录
//    @Scheduled(cron = "0/10 * * * * ?")
    public void messageSendTask() {
        List<QueueMessages> queueMessages = queueMessagesMapper.selectList(new QueryWrapper<QueueMessages>().eq("status", MessageStatusEnum.RETRYSTATUS.getCode()).lt("try_time", new Date()));
        //TODO 此处可以使用链式写法，后期改
        queueMessages.forEach(s->{
            if (s.getCount()>=3){
                //遍历所有消息,如果重试次数count大于等于3,则直接设置这条消息发送失败,status = 2
                queueMessagesMapper.updateStatusByMessageId(s.getMessageId(),MessageStatusEnum.ERRORSTATUS.getCode());
            }else {
                System.out.println("重试消息"+s);
                //如果重试次数count<3,则根据message_id和获取当前时间更新date来更新这条消息的重试次数count+1
                queueMessagesService.updateCountByMessageId(s.getMessageId());
                //获取消息体content,并将获取到的json字符串解析成对象,然后通过rabbitTemplate把对象投递到rabbitmq
                QueueMessages messages = JSON.parseObject(s.getContent(), QueueMessages.class);
                //投递消息
                rabbitTemplate.convertAndSend(messages.getExchange(), messages.getRouteKey(), messages, new CorrelationData(messages.getMessageId().toString()));
            }
        });

//        if (queueMessages != null) {
//            for (QueueMessages queueMessage : queueMessages) {
//                if (queueMessage.getCount() >= 3) {
//                    queueMessagesMapper.update(new QueueMessages(), new UpdateWrapper<QueueMessages>().set("status", MessageStatusEnum.ERRORSTATUS.getCode()).eq("messageId", queueMessage.getMessageId()));
//                } else{
//                    queueMessagesService.updateCountByMessageId(queueMessage.getMessageId());
////                    queueMessagesMapper.update(new QueueMessages(), new UpdateWrapper<QueueMessages>().set("updated_at",new Date()).and(new Consumer<UpdateWrapper<QueueMessages>>() {
////                                @Override
////                                public void accept(UpdateWrapper<QueueMessages> queueMessagesUpdateWrapper) {
////                                    if (queueMessage.getCount()> 1){
////                                        queueMessagesUpdateWrapper.set("count",queueMessage.getCount()+1);
////                                    }
////                                }
////                            })
////                            .eq("message_id", queueMessage.getMessageId()));
////                }
//                    //获取消息体content,并将获取到的json字符串解析成对象,然后通过rabbitTemplate把对象投递到rabbitmq
//                    QueueMessages messages = JSON.parseObject(queueMessage.getContent(), QueueMessages.class);
//                    //投递消息
//                    rabbitTemplate.convertAndSend(messages.getExchange(), messages.getRouteKey(), messages, new CorrelationData(messages.getMessageId().toString()));
//                }
//            }
//        }
    }
}
